package com.ruoyi.project.bus.compare.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.bus.compare.domain.BusCompare;
import com.ruoyi.project.bus.compare.service.IBusCompareService;
import com.ruoyi.project.bus.register.domain.BusRegister;
import com.ruoyi.project.bus.register.service.IBusRegisterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * compareController
 * 
 * @author ruoyi
 * @date 2020-11-28
 */
@Controller
@RequestMapping("/bus/compare")
public class BusCompareController extends BaseController
{
    private String prefix = "bus/compare";

    @Autowired
    private IBusCompareService busCompareService;
    @Autowired
    private IBusRegisterService busRegisterService;

    @RequiresPermissions("bus:compare:view")
    @GetMapping()
    public String compare()
    {
        return prefix + "/compare";
    }

    /**
     * 查询compare列表
     */
    @RequiresPermissions("bus:compare:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusCompare busCompare)
    {
        startPage();
        List<BusCompare> list = busCompareService.selectBusCompareList(busCompare);
        return getDataTable(list);
    }

    /**
     * 导出compare列表
     */
    @RequiresPermissions("bus:compare:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusCompare busCompare)
    {
        List<BusCompare> list = busCompareService.selectBusCompareList(busCompare);
        ExcelUtil<BusCompare> util = new ExcelUtil<BusCompare>(BusCompare.class);
        return util.exportExcel(list, "compare");
    }

    /**
     * 新增compare
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存compare
     */
    @RequiresPermissions("bus:compare:add")
    @Log(title = "compare", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusCompare busCompare)
    {
        return toAjax(busCompareService.insertBusCompare(busCompare));
    }

    /**
     * 修改compare
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BusCompare busCompare = busCompareService.selectBusCompareById(id);
        mmap.put("busCompare", busCompare);
        return prefix + "/edit";
    }

    /**
     * 修改保存compare
     */
    @RequiresPermissions("bus:compare:edit")
    @Log(title = "compare", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusCompare busCompare)
    {
        return toAjax(busCompareService.updateBusCompare(busCompare));
    }

    /**
     * 删除compare
     */
    @RequiresPermissions("bus:compare:remove")
    @Log(title = "compare", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busCompareService.deleteBusCompareByIds(ids));
    }

    @GetMapping("/compareData")
    @Log(title = "compare", businessType = BusinessType.OTHER)
    public String compareData(String id, ModelMap modelMap) {
        modelMap.put("compareId", id);
        return prefix + "/compareData";
    }

    @PostMapping("/compareData")
    @Log(title = "compare", businessType = BusinessType.OTHER)
    @ResponseBody
    public TableDataInfo compareData(@RequestParam("id") Long id) {
        List<BusRegister> busRegisters = busRegisterService.compareData(id);
        return getDataTable(busRegisters);
    }

    @Log(title = "比对数据导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCompareData")
    @ResponseBody
    public AjaxResult export(@RequestParam("id") Long id) {
        List<BusRegister> busRegisters = busRegisterService.compareData(id);
        ExcelUtil<BusRegister> registerExcelUtil = new ExcelUtil<>(BusRegister.class);
        return registerExcelUtil.exportExcel(busRegisters, "比对差异数据");
   }
}
